package top.xiaozhaobk.xiaofenshu.user.biz.exception;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.xiaozhaobk.framework.common.exception.BizException;
import top.xiaozhaobk.framework.common.response.Response;
import top.xiaozhaobk.xiaofenshu.user.biz.enums.ResponseCodeEnums;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 捕获自定义异常
     * @return
     */
    @ExceptionHandler({BizException.class})
    @ResponseBody// 响应JSON, 不返回页面
    public Response<String> handleBizException(HttpServletRequest request, BizException e) {
        log.warn("{} request fail, errorCode: {}, errorMessage: {}", request.getRequestURI(), e.getErrorCode(), e.getErrorMessage());
        return Response.fail(e);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response<Object> handleOtherException(HttpServletRequest request, Exception e) {
        log.error("==> 全局异常处理, url: {}, message: {}", request.getRequestURI(), e.getMessage());
        return Response.fail(ResponseCodeEnums.SYSTEM_ERROR);
    }
}
